<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\User;
use app\common\model\UserCode;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use think\Response;

class Register extends Base
{
    public function index(): string
    {
        return View::fetch();
    }

    public function save(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Register');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }
        //校验短信验证码
        $res = UserCode::verifyMsgCode($param['mobile'], $param['captcha'], 'register');
        if ($res !== true) return Result::fail($res);

        return Result::fastReturn(User::_save($param, ''));
    }
}